unit uControls;

interface

Uses ExtCtrls, StdCtrls, ComCtrls, Controls, Classes, Math, Graphics;

procedure SetROEdit(AEdit: TEdit; RO: Boolean);
procedure SetROProperty(AControl: TObject; const RO: Boolean);
procedure SetBoolProperty(AControl: TObject; APropName: string; const AValue: Boolean);
function HasProperty(AControl: TObject; APropName: string): Boolean;
function GetBoolProperty(AControl: TObject; APropName: string): Boolean;

procedure SetDate(Adtp: TDateTimePicker; ADate: TDateTime);
function GetDate(Adtp: TDateTimePicker): TDateTime;
function DateToStr0(ADate: TDateTime): string;

procedure SetComboItem(cmb: TComboBox; Id: Integer);
procedure SetComboItemU(cmb: TComboBox; Id: Cardinal);
function GetComboItem(cmb: TComboBox): Integer;
function GetComboItemU(cmb: TComboBox): Cardinal;

procedure SetListItem(cmb: TListBox; Id: Integer);
function GetListItem(cmb: TListBox): Integer;

function GetObjId(control: TRadioGroup): Integer;
procedure SetObjId(control: TRadioGroup; AIndex: Integer);

function GetControlSelectedData(cntrl: TControl): Integer;

procedure AddColumn(AText: string; AWidth: Integer; lv: TListView);


implementation

Uses SysUtils, TypInfo;

procedure SetROEdit(AEdit: TEdit; RO: Boolean);
begin
  AEdit.ReadOnly := RO;
  AEdit.Color := IfThen(RO, clBtnFace, clWindow);
end;

procedure SetROProperty(AControl: TObject; const RO: Boolean);
var
  i: Integer;
  NumProps: word;
  TypeInf: PTypeInfo;
  TypeData: PTypeData;
  PropList: PPropList;
  PropInfo: PPropInfo;
begin
  TypeInf := AControl.ClassInfo;
  TypeData := GetTypeData(TypeInf);
  NumProps := TypeData^.PropCount;
  GetMem(PropList, NumProps*SizeOf(Pointer));
  try
    GetPropInfos(TypeInf, PropList);
    for i := 0 to NumProps - 1 do begin
      PropInfo := PropList^[i];
      if SameText(string(PropInfo.Name), 'ReadOnly') then
        SetInt64Prop(AControl, PropInfo, Int64(RO));
    end;
  finally
    FreeMem(PropList);
  end;
end;

procedure SetBoolProperty(AControl: TObject; APropName: string; const AValue: Boolean);
var
  i: Integer;
  NumProps: word;
  TypeInf: PTypeInfo;
  TypeData: PTypeData;
  PropList: PPropList;
  PropInfo: PPropInfo;
begin
  TypeInf := AControl.ClassInfo;
  TypeData := GetTypeData(TypeInf);
  NumProps := TypeData^.PropCount;
  GetMem(PropList, NumProps*SizeOf(Pointer));
  try
    GetPropInfos(TypeInf, PropList);
    for i := 0 to NumProps - 1 do begin
      PropInfo := PropList^[i];
      if SameText(string(PropInfo.Name), APropName) then
        SetOrdProp(AControl, PropInfo, Int64(AValue));
    end;
  finally
    FreeMem(PropList);
  end;
end;

function HasProperty(AControl: TObject; APropName: string): Boolean;
var
  i: Integer;
  NumProps: word;
  TypeInf: PTypeInfo;
  TypeData: PTypeData;
  PropList: PPropList;
  PropInfo: PPropInfo;
begin
  Result := False;
  TypeInf := AControl.ClassInfo;
  TypeData := GetTypeData(TypeInf);
  NumProps := TypeData^.PropCount;
  GetMem(PropList, NumProps*SizeOf(Pointer));
  try
    GetPropInfos(TypeInf, PropList);
    for i := 0 to NumProps - 1 do begin
      PropInfo := PropList^[i];
      if SameText(string(PropInfo.Name), APropName) then
        begin
          Result := True;
          Exit;
        end;
    end;
  finally
    FreeMem(PropList);
  end;
end;

function GetBoolProperty(AControl: TObject; APropName: string): Boolean;
begin
  Result := Boolean(GetOrdProp(AControl, APropName));
end;

procedure SetDate(Adtp: TDateTimePicker; ADate: TDateTime);
begin
//  Adtp.ShowCheckbox := ADate = 0;
  if ADate = 0 then
    begin
      Adtp.Date := Date;
      Adtp.Checked := False;
    end
  else begin
    Adtp.Date := ADate;
    Adtp.Checked := True;
  end;
end;

function GetDate(Adtp: TDateTimePicker): TDateTime;
begin
  if Adtp.ShowCheckbox then
    begin
      if not Adtp.Checked then
        Result := 0
      else
        Result := Adtp.Date;
    end
  else
    Result := Adtp.Date;
end;

function DateToStr0(ADate: TDateTime): string;
begin
  if ADate = 0 then
    Result := ''
  else
    Result := DateToStr(ADate);
end;

procedure SetComboItem(cmb: TComboBox; Id: Integer);
var
  Index: Integer;
begin
  Index := cmb.Items.IndexOfObject(TObject(ID));
  cmb.ItemIndex := Index;
end;

procedure SetComboItemU(cmb: TComboBox; Id: Cardinal);
var
  Index: Integer;
begin
  Index := cmb.Items.IndexOfObject(TObject(ID));
  cmb.ItemIndex := Index;
end;

function GetComboItem(cmb: TComboBox): Integer;
begin
  if cmb.ItemIndex = -1 then
    Result := 0
  else
    Result := Cardinal(cmb.Items.Objects[cmb.ItemIndex]);
end;

function GetComboItemU(cmb: TComboBox): Cardinal;
begin
  if cmb.ItemIndex = -1 then
    Result := 0
  else
    Result := Cardinal(cmb.Items.Objects[cmb.ItemIndex]);
end;

procedure SetListItem(cmb: TListBox; Id: Integer);
var
  Index: Integer;
begin
  Index := cmb.Items.IndexOfObject(TObject(ID));
  cmb.ItemIndex := Index;
end;

function GetListItem(cmb: TListBox): Integer;
begin
  if cmb.ItemIndex = -1 then
    Result := 0
  else
    Result := Integer(cmb.Items.Objects[cmb.ItemIndex]);
end;

function GetObjId(control: TRadioGroup): Integer;
begin
  if control.ItemIndex = -1 then
    Result := -1
  else
    Result := Cardinal(control.Items.Objects[control.ItemIndex]);
end;

procedure SetObjId(control: TRadioGroup; AIndex: Integer);
begin
  AIndex := control.Items.IndexOfObject(TObject(AIndex));
  control.ItemIndex := AIndex;
end;

function GetControlSelectedData(cntrl: TControl): Integer;
begin
  Result := -1;
  if cntrl is TComboBox then begin
    if TComboBox(cntrl).ItemIndex <> -1 then
      Result := Integer(TComboBox(cntrl).Items.Objects[TComboBox(cntrl).ItemIndex]);
  end else if cntrl is TListView then begin
    if Assigned(TListView(cntrl).Selected) then
      Result := Integer(TListView(cntrl).Selected.Data);
  end;
end;

procedure AddColumn(AText: string; AWidth: Integer; lv: TListView);
var
  c: TListColumn;
begin
  c := lv.Columns.Add;
  c.Caption := AText;
  c.Width := AWidth;
end;

end.